package atop;

import java.util.Arrays;

/**
 * @创建人 MBY381
 * @创建时间 2022/4/19 21:02
 * @描述 暂无
 */
public class QuickSortMyVersion {
    public static void main(String[] args) {
        System.out.println("正确答案：    [1, 2, 3, 4, 5, 6, 7, 8, 9, 21, 227, 259, 381, 696, 782]");
        int[] nums = new int[]{0xE3, 381, 8, 6, 4, 7, 9, 3, 5, 2, 1, 782, 696, 259, 0x15};
        System.out.println(getPartition(nums, 12));
        System.out.println(Arrays.toString(nums));
    }

    static int target;
    static int answer = 0;

    public static int getPartition(int[] nums, int k) {
        target = k - 1;
        sort(nums, 0, nums.length - 1);
        return answer;
    }


    public static void sort(int[] nums, int start, int end) {
        if (start > end) {
            return;
        }
        int value = nums[start];
        int left = start, right = end;
        while (left < right) {
            while (nums[right] >= value && left < right) {
                right--;
            }
            while (nums[left] <= value && left < right) {
                left++;
            }
            if (left < right) {
                swap(nums, left, right);
            }
        }
        swap(nums, start, left);
        if (left == target) {
            System.out.println("target:" + target);
            answer = nums[left];
            return;
        } else if (left < target) {
            sort(nums, left + 1, end);
        } else {                            //if(left>target)
            sort(nums, start, left - 1);
        }
    }

//        if(left==right&&nums[left]<=value){
//            swap(nums,start,left);
//            System.out.println(1+""+""+left+" "+value+"start:"+start);
//            sort(nums,start,left-1);
//            sort(nums,start,left+1);
//        }else {
//            swap(nums,start,left-1);
//            System.out.println(2);
//            sort(nums,left,end-1);
//            sort(nums,start,left-2);
////            return left-1;
//        }

    public static void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }
}
